<?php
include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/function.php');

$opts = getopt('', ['min:', 'max:']);
if(!$opts['min'] || !$opts['max']) {
    echo2('--min --max required', 'QISUU_REPLY');
    sleep(5);
    exit;
}

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(10);

$url_tpl = 'http://www.lnsuu.com/forum.php?mod=viewthread&tid=%s';
$users   = file('./qisuu_all/lnsuu_user.txt');
$replys  = file('./qisuu_all/lnsuu_reply.txt');

$count = count($users);
$k = 0;
echo2('total users '.$count, 'QISUU_REPLY');
while (true) {
    $stop = $redis->get('QISUU_QUEUE_STOP');
    if($stop) {
        echo2('stop', 'QISUU_POST'); break;
    }

    $tid = mt_rand($opts['min'], $opts['max']);
    $url = sprintf($url_tpl, $tid);

    shuffle($users);
    $user = $users[0];
    //$user = $users[$k];
    $user = preg_split('/\s+/', $user);

    if($k >= $count) {
        echo2("$k end", 'QISUU_REPLY');
        break;
    }

    shuffle($replys);
    $reply = trim($replys[0]);

    $content  = $reply;
    $username = $user[0];
    $password = $user[1];

    $shell = "LC_ALL=en_US.UTF-8 /usr/bin/casperjs lnsuu_reply.js '$content' '$username' '$password' '$url'";
    echo2($k . ' ' . $shell, 'QISUU_REPLY');
    $result = shell_exec($shell);
    //echo2($result);
    
    $k++;
    $t = mt_rand(2,15);
    echo2("sleep $t", 'QISUU_REPLY');
    sleep($t);
}

sleep(600);

echo2('sleep exit', 'QISUU_REPLY');
